﻿@model JPSOFTWARE.BON.Modelos.DistribuidoraIncluirModel
@{
    ViewBag.Title = "Bebida Social - Registro de Distribuidora";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<script src="@Url.Content("~/Scripts/GestaoDistribuidora/Distribuidora.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.alphanumeric.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.maskedinput-1.2.2.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.cep-1.0.js")" type="text/javascript"></script>
<script src="@Url.Content("https://maps.google.com/maps/api/js?sensor=true&region=BR")" type="text/javascript"></script>
<script type="text/javascript">
    jQuery(function ($) {
        $("#Cnpj").mask("99.999.999/9999-99");
        $("#Telefone").mask("(99)9999-9999");
        $("#Celular").mask("(99)9999-9999");
        $("#Cep").mask("99999-999");
        $("#Latitude").attr('readonly', true);
        $("#Longitude").attr('readonly', true);

        $("#Loading").hide();

        $("#Cep").keyup(function () {
            $("#Cep").cep({
                load: function () {
                    //Exibe a div loading
                    $("#Loading").show();
                },
                complete: function () {
                    //Esconde a div loading
                    $("#Loading").hide();
                },
                error: function (msg) {
                    //Exibe em alert a mensagem de erro retornada
                    alert(msg);
                    $('#Endereco').val("");
                    $('#Bairro').val("");
                    $('#Cidade').val("");
                    $('#Uf').val("");
                },
                success: function (data) {
                    $('#Endereco').val(data.tipoLogradouro + ' ' + data.logradouro);
                    $('#Bairro').val(data.bairro);
                    $('#Cidade').val(data.cidade);
                    $('#Uf').val(data.estado);
                }
            });
        });
        Initializa();
        IncluirDistribuidora();
    });
    var markersArray = [];
    var lat;
    var lon;
    var image = "../../../../Content/bootstrap/img/marker_Distribuidora.png";
    var map;

    function Initializa() {
        var posicaoInicial = new google.maps.LatLng(-15.819621034094615, -47.94099775164892);
        var myOptions = {
            zoom: 10,
            center: posicaoInicial,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            animation: google.maps.Animation.BOUNCE
        };
        map = new google.maps.Map(document.getElementById("map"), myOptions);
        google.maps.event.addListener(map, 'click', function (event) {
            placeMarker(event.latLng);
            $("#Latitude").val(event.latLng.lat());
            $("#Longitude").val(event.latLng.lng());
        });
    }

    // Removes the overlays from the map, but keeps them in the array
    function clearOverlays() {
        if (markersArray) {
            for (i in markersArray) {
                markersArray[i].setMap(null);
            }
            markersArray.length = 0;
        }
    }

    function placeMarker(location) {
        debugger;
        clearOverlays();
        var marker = new google.maps.Marker({
            position: location,
            map: map,
            icon: image,
            title: "Sua distribuidora está aqui?"
        });
        markersArray.push(marker);
        //map.setMap(location);
    }
</script>
<div class="span2"></div>
<div>
    <div class="span8 box">
        <div class="box-head">
            <h3>Registro de Distribuidora</h3>
        </div>
        <div class="box-content">
            <div class="content-inner">
                <div class="form-horizontal">
                    @using (Html.BeginForm("IncluirDistribuidora", "Distribuidora", FormMethod.Post, new { id = "formIncluirDistribuidora" }))
                    {   
                        @Html.Hidden("hDistribuidoraInserida", ViewData["DistribuidoraInserida"])
                        @Html.Hidden("hDistribuidoraErro", ViewData["DistribuidoraErro"])

                        @Html.ValidationSummary(true)   
                        <legend>Dados da Distribuidora</legend>                     
                        <div class="control-group">
                            <div class="control-label">
                                @Html.LabelFor(model => model.Cnpj)
                            </div>
                            <div class="controls">
                                @Html.TextBoxFor(model => model.Cnpj, new { @class = "input-large", MaxLength = 19 })
                                @Html.ValidationMessageFor(model => model.Cnpj)
                            </div>
                        </div>
                        <div class="control-group">
                            <div class="control-label">
                                @Html.LabelFor(model => model.NomeDistribuidora)
                            </div>
                            <div class="controls">
                                @Html.TextBoxFor(model => model.NomeDistribuidora, new { @class = "input-xxlarge", MaxLength = 70 })
                                @Html.ValidationMessageFor(model => model.NomeDistribuidora)
                            </div>
                        </div>
                        <div class="control-group">
                            <div class="control-label">
                                @Html.LabelFor(model => model.Descricao)
                            </div>
                            <div class="controls">
                                @Html.TextAreaFor(model => model.Descricao, new { @class = "input-xxlarge", MaxLength = 1000 })
                                @Html.ValidationMessageFor(model => model.Descricao)
                            </div>
                        </div>
                        <div class="control-group">
                            <div class="control-label">
                                @Html.LabelFor(model => model.Telefone)
                            </div>
                            <div class="controls">
                                @Html.TextBoxFor(model => model.Telefone, new { @class = "input-large", MaxLength = 13 })
                                @Html.ValidationMessageFor(model => model.Telefone)
                            </div>
                        </div>
                        <div class="control-group">
                            <div class="control-label">
                                @Html.LabelFor(model => model.Celular)
                            </div>
                            <div class="controls">
                                @Html.TextBoxFor(model => model.Celular, new { @class = "input-large", MaxLength = 13 })
                                @Html.ValidationMessageFor(model => model.Celular)
                            </div>
                        </div>
                        <div class="control-group">
                            <div class="control-label">
                                @Html.LabelFor(model => model.Email)
                            </div>
                            <div class="controls">
                                @Html.TextBoxFor(model => model.Email, new { @class = "input-large", MaxLength = 70 })
                                @Html.ValidationMessageFor(model => model.Email)
                            </div>
                        </div>
                        <legend>Endereço da Distribuidora</legend>
                        <div class="control-group">
                            <div class="control-label">
                                @Html.LabelFor(model => model.Cep)
                            </div>
                            <div class="controls">
                                @Html.TextBoxFor(model => model.Cep, new { @class = "input-large", MaxLength = 9 })
                                @Html.ValidationMessageFor(model => model.Cep)
                                <div id="Loading">
                                    <img src="@Url.Content("~/Content/bootstrap/img/loading.gif")" alt="Carregando" />
                                </div>
                            </div>
                        </div>
                        <div class="control-group">
                            <div class="control-label">
                                @Html.LabelFor(model => model.Endereco)
                            </div>
                            <div class="controls">
                                @Html.TextAreaFor(model => model.Endereco, new { @class = "input-xxlarge", MaxLength = 250 })
                                @Html.ValidationMessageFor(model => model.Endereco)
                            </div>
                        </div>
                        <div class="control-group">
                            <div class="control-label">
                                @Html.LabelFor(model => model.Bairro)
                            </div>
                            <div class="controls">
                                @Html.TextBoxFor(model => model.Bairro, new { @class = "input-xlarge", MaxLength = 70 })
                                @Html.ValidationMessageFor(model => model.Bairro)
                            </div>
                        </div>
                        <div class="control-group">
                            <div class="control-label">
                                @Html.LabelFor(model => model.Cidade)
                            </div>
                            <div class="controls">
                                @Html.TextBoxFor(model => model.Cidade, new { @class = "input-xlarge", MaxLength = 50 })
                                @Html.ValidationMessageFor(model => model.Cidade)
                            </div>
                        </div>
                        <div class="control-group">
                            <div class="control-label">
                                @Html.LabelFor(model => model.Uf)
                            </div>
                            <div class="controls">
                                @Html.TextBoxFor(model => model.Uf, new { @class = "input-large", MaxLength = 2 })
                                @Html.ValidationMessageFor(model => model.Uf)
                            </div>
                        </div>
                        <legend>Localização da Distribuidora</legend><small style="color: red;">- Marque no mapa onde sua distribuidora esta localizada. Isso facilitará sua localização para os clientes.</small>
                        <div id="map" style="height: 275px;">
                        </div>
                        <div class="control-group" style="padding-top: 35px;">
                            <div class="control-label">
                                @Html.LabelFor(model => model.Latitude)
                            </div>
                            <div class="controls">
                                @Html.TextBoxFor(model => model.Latitude, new { @class = "input-large", MaxLength = 20 })
                                @Html.ValidationMessageFor(model => model.Latitude)
                            </div>
                        </div>
                        <div class="control-group">
                            <div class="control-label">
                                @Html.LabelFor(model => model.Longitude)
                            </div>
                            <div class="controls">
                                @Html.TextBoxFor(model => model.Longitude, new { @class = "input-large", MaxLength = 20 })
                                @Html.ValidationMessageFor(model => model.Longitude)
                            </div>
                        </div>
                        <div class="form-actions">
                            <button type="submit" class="btn btn-primary"><i class="icon-ok icon-white"></i>&nbsp;Registrar Distribuidora</button>
                            <button type="button" class="btn" id="btCancelar"><i class="icon-ban-circle"></i>&nbsp;Cancelar</button>
                        </div>                        
                    }
                </div>
            </div>
        </div>
    </div>
</div>
<div class="span2"></div>
<div id="divIncluirDistribuidoraSucesso" class="modal">
    <div class="modal-header">
        <h3>Registro de Distribuidora</h3>
    </div>
    <div class="modal-body">
        @ViewData["Mensagem"]
    </div>
    <div class="modal-footer">
        @*@Html.ActionLink("Nova Distribuidora?", "IncluirDistribuidora", "Distribuidora", new { area = "GestaoDistribuidora" }, new { @class = "btn btn-primary" })*@
        @Html.ActionLink("Vizualizar Distribuidora", "MinhaDistribuidora", "Distribuidora", new { Area = "GestaoDistribuidora" }, new { @class = "btn btn-primary" })
    </div>
</div>
<div id="divIncluirDistribuidoraErro" class="modal">
    <div class="modal-header">
        <h3>Registro de Distribuidora</h3>
    </div>
    <div class="modal-body">
        @Html.ViewData["DistribuidoraErro"]
    </div>
    <div class="modal-footer">
        <span><a class="btn btn-primary" href="javascript:FecharTelaIncluirDistribuidoraErro()">Tentar
            novamente?</a></span>
    </div>
</div>
<div id="divIncluirDistribuidoraCancelar" class="modal">
    <div class="modal-header">
        <h3>Registro de Distribuidora</h3>
    </div>
    <div class="modal-body">
        @JPSOFTWARE.BON.Modelos.Mensagens.MSG_020
    </div>
    <div class="modal-footer">
        <span>@Html.ActionLink("Sim", "MinhaDistribuidora", new { area = "GestaoDistribuidora" }, new { @class = "btn btn-primary" })</span>
        <span><a class="btn" href="javascript:CancelarOperacaoIncluir()">Não</a></span>
    </div>
</div>

@*http://mapicons.nicolasmollet.com/category/markers/restaurants-bars/?custom_color=1e3cfa&style=default*@